package com.k2.gulimall.member.controller;

import java.io.Serializable;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.entity.MemberLevelEntity;
import com.k2.gulimall.member.service.MemberDetailsService;
import com.k2.gulimall.member.service.MemberLevelService;
import com.k2.gulimall.member.service.MemberService;
import com.k2.gulimall.member.vo.MemberDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.GrowthChangeHistoryEntity;
import com.k2.gulimall.member.service.GrowthChangeHistoryService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;



/**
 * 成长值变化历史记录
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/growthchangehistory")
public class GrowthChangeHistoryController {
    @Autowired
    private GrowthChangeHistoryService growthChangeHistoryService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberLevelService memberLevelService;
    @Autowired
    MemberDetailsService memberDetailsService;

    //当前时间
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String dateString = df.format(new Date());
    ParsePosition pos = new ParsePosition(8);
    Date data;
    {
        try {
            data = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = growthChangeHistoryService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		GrowthChangeHistoryEntity growthChangeHistory = growthChangeHistoryService.getById(id);

        return R.ok().put("growthChangeHistory", growthChangeHistory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GrowthChangeHistoryEntity growthChangeHistory){
        //设置当前时间
        growthChangeHistory.setCreateTime(data);
        //设置更改后的成长值

        //设置
		growthChangeHistoryService.save(growthChangeHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GrowthChangeHistoryEntity growthChangeHistory){
		growthChangeHistoryService.updateById(growthChangeHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		growthChangeHistoryService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 给会员添加成长值记录
     * @param memberDetailsVo
     * @return
     */
    @PostMapping("/addMemberGrowthChange")
    public R addMemberGrowthChange(@RequestBody MemberDetailsVo memberDetailsVo){
        Long memberId = memberDetailsVo.getId();
        Integer change = memberDetailsVo.getGrowth();
        //获取会员信息
        MemberEntity memberEntity = memberService.getById(memberId);
        //添加会员成长值记录
        GrowthChangeHistoryEntity newGrowthChangeHistory = new GrowthChangeHistoryEntity();
        newGrowthChangeHistory.setCreateTime(data);//创建时间
        newGrowthChangeHistory.setMemberId(memberId);//会员id
        if(change!=null){//管理员添加
            newGrowthChangeHistory.setChangeCount(change-memberEntity.getGrowth());//更改的成长值
            memberEntity.setGrowth(change);
            newGrowthChangeHistory.setSourceType(1);//添加者
        }else{//购物添加
            newGrowthChangeHistory.setChangeCount(2);
            memberEntity.setGrowth(memberEntity.getGrowth()+2);
            newGrowthChangeHistory.setSourceType(0);
        }
        //判断会员的等级
        MemberLevelEntity memberLevelEntity = memberLevelService.getMemberLevelToGrowth(memberEntity.getGrowth());
        //更改等级id
        memberEntity.setLevelId(memberLevelEntity.getId());
        //添加成长值记录，保存更改后的成长值
        if (growthChangeHistoryService.save(newGrowthChangeHistory)&&memberService.saveOrUpdate(memberEntity)){
            return R.ok().put("code",0).put("data",memberDetailsService.getMemberDetails(memberEntity.getId()));
        }else{
            return R.error(1,"请求参数错误");
        }
    }





}
